
package org.springboot.szsrcb.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Req_workload_info;
import org.springboot.szsrcb.entity.Test_base_info;
import org.springboot.szsrcb.entity.Test_bill;
import org.springboot.szsrcb.entity.Test_testing_info;
import org.springboot.szsrcb.entity.Test_workload_info;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Query_Service;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.Test_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/staffer")
public class StafferRestController_query {

	private static final Logger logger = LoggerFactory.getLogger(StafferRestController_query.class);

	@Autowired
	
	private Req_bill_Service req_bill_Service;

	@Autowired
	private Test_bill_Service test_bill_Service;

	@Autowired
	private Query_Service query_Service;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/query_staffer_form_load")
	public String query_staffer_form_load(HttpServletRequest request) {
		// List<User> list_u=userService.getAllUserList();
		String json_all_user = userService.trans_all_user_2_json();

		String r_start = "["

				+ "{type:'fieldset',label:'人员查询统计---查询条件',list:[" + "{type:'block',list:["
				+ "{type : 'calendar',name : 'query_date_start',label : '工作量计算日期',inputWidth:'120',labelWidth:'100',position:'label-left', value:''},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'calendar',name : 'query_date_end',label : '-',inputWidth:'120',position:'label-left', offsetLeft:'25',value:''}"
				+ "]}," + "{type:'block',list:["
				+ "{type : 'select',name : 'project_status',label : '项目状态',inputWidth:'120',labelWidth:'100',options:["
				+ "{text: '', value: ''}," + "{text: '已接收', value: '已接收'}," + "{text: '已退回', value: '已退回'},"
				+ "{text: '已完成', value: '已完成'}," + "{text: '已分配',value: '已分配'}" + "]}," + "{type : 'newcolumn'},"
				+ "{type:'checkbox', name:'is_expire', label:'已逾期', offsetLeft:'25',  position:'label-right', width:'150'}"
				+ "]}," + "{type: 'block', list:["
				+ "{type:'multiselect',label: '选择人员', position: 'label-left', name:'req_analyst',required:'true', size:7,width:120, options:["
				+ "" + "]}," + "{type : 'newcolumn'}," + "{type:'block',list:["
				+ "{type: 'button', name: 'add2', value: '>>', width:50},"
				+ "{type: 'button', name: 'remove2', width:50,value:'<<'},"
				+ "{type: 'button', name: 'select_all', width:50,value:'全选'},"
				+ "{type: 'button', name: 'cancel_all', width:50,value:'全取消'}" + "]}," + "{type : 'newcolumn'},"
				+ "{type:'multiselect', name:'req_analyst_all', size:7, width:100,offsetLeft:'25',options:["
				+ json_all_user + "]}" + "]},"

				+ "{type:'block',list:[" + "{type : 'button',name : 'search',value : '查询'}," + "{type : 'newcolumn'},"
				+ "{type : 'button',name : 'search_reset',value : '重置'}" + "]}," + "]},"
				+ "{type:'fieldset',label:'需求项目合计',list:["

				+ "{type:'block',list:[" + "{type : 'label',name : 'display_label2',label : '需求项目数量 ：'},"
				+ "{type : 'newcolumn'}," + "{type : 'label',name : 'req_count',label : ''}," + "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label3',label : '个'}" + "]}," + "{type:'block',list:["
				+ "{type : 'label',name : 'display_label4',label : '计划工作量合计  ：'}," + "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'req_plan_workload_count',label : ''}," + "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label5',offsetLeft:'25',label : '人天'}," + "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label6',offsetLeft:'25',label : '实际工作量合计  ：'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'req_act_workload_count',offsetLeft:'25',label : ''},"
				+ "{type : 'newcolumn'}," + "{type : 'label',name : 'display_label7',offsetLeft:'25',label : '人天'}"
				+ "]}," + "]}," + "{type:'fieldset',label:'测试项目合计',list:[" + "{type:'block',list:["
				+ "{type : 'label',name : 'display_label2',label : '测试项目数量 ：'}," + "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'test_count',label : ''}," + "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label3',label : '个'}" + "]}," + "{type:'block',list:["
				+ "{type : 'label',name : 'display_label4',label : '测试案例合计：',labelWidth:'120'},"
				+ "{type : 'newcolumn'}," + "{type : 'label',name : 'test_case_count',label : '',labelWidth:'50'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label5',offsetLeft:'25',label : '个',labelWidth:'50'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label6',offsetLeft:'50',label : '发现缺陷数  ：',labelWidth:'100'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'deffect_count',offsetLeft:'25',label : '',labelWidth:'50'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label7',offsetLeft:'25',label : '个',labelWidth:'50'}" + "]},"
				+ "{type:'block',list:["
				+ "{type : 'label',name : 'display_label4',label : '计划工作量合计  ：'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'test_plan_workload_count',label : '',labelWidth:'50'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label5',offsetLeft:'25',label : '人天',labelWidth:'50'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label6',offsetLeft:'50',label : '实际工作量合计  ：'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'test_act_workload_count',offsetLeft:'25',label : '',labelWidth:'50'},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'display_label7',offsetLeft:'25',label : '人天',labelWidth:'50'}" + "]}" + "]},"
				/*
				*/
				+ "]";
		String r_s = r_start;
		logger.info("retrun team info :" + r_start);
		return r_s;

	}

	@RequestMapping(value = "/query_test_grid")
	public String query_test_grid(HttpServletRequest request) throws ParseException {
		logger.info("access /req/query_test_grid");
		String g_test_ids = (String) request.getParameter("test_ids");
		String s_tmp = "";
		if (g_test_ids != null) {
			g_test_ids = g_test_ids.replace("_", ",");
			s_tmp = json_query_test_grid(g_test_ids);
			logger.info("query_test_grid :" + s_tmp);
		}
		return s_tmp;

	}

	private String json_query_test_grid(String g_test_ids) {
		String[] array = g_test_ids.split(",");
		String r_s = "";
		String r_start = "{rows:[";
		String r_end = "]}";

		for (int i = 0; i < array.length; i++) {

			String tmp_start = "{ id:" + (i + 1) + ",data:[";
			String tmp_end = "]}";
			String tmp_content = "";

			String g_test_id = array[i];
			Test_bill test_bill = test_bill_Service.search_a_test_bill_by_id(g_test_id);
			Req_bill req_bill = req_bill_Service.req_bill_search_by_id(String.valueOf(test_bill.getReq_id()));
			// radio
			tmp_content = tmp_content + "'0',";
			// Req_ID
			tmp_content = tmp_content + "'" + test_bill.getId() + "',";
			// 需求编号
			tmp_content = tmp_content + "'" + req_bill.getReq_bill_no() + "',";
			// 需求主题
			tmp_content = tmp_content + "'" + req_bill.getReq_title() + "',";
			// 需求状态
			tmp_content = tmp_content + "'" + test_bill.getTest_status() + "',";
			Test_base_info test_base_info = test_bill_Service.search_a_test_base_info_by_testid(g_test_id);
			if (test_base_info != null) {
				// 需求负责人
				tmp_content = tmp_content + "'" + test_base_info.getTest_head() + "',";
				// 需求提出部门
				// tmp_content = tmp_content + "'" +
				// test_bill.getReq_offer_dept() + "',";
				// 要求完成日期
				if (test_bill.getTest_complete_date() == null) {
					tmp_content = tmp_content + "'',";
				} else {
					tmp_content = tmp_content + "'" + sdf.format(test_bill.getTest_complete_date()) + "',";
				}

				// 计划完成日
				if (test_base_info.getTest_plan_date() == null) {
					tmp_content = tmp_content + "'',";
				} else {
					tmp_content = tmp_content + "'" + sdf.format(test_base_info.getTest_plan_date()) + "',";
				}

				// 实际完成日
				if (test_base_info.getActual_finish_date() == null) {
					tmp_content = tmp_content + "'',";
				} else {
					tmp_content = tmp_content + "'" + sdf.format(test_base_info.getActual_finish_date()) + "',";
				}
				// 接收日
				if (test_bill.getCreate_date() == null) {
					tmp_content = tmp_content + "'',";
				} else {
					tmp_content = tmp_content + "'" + sdf.format(test_bill.getCreate_date()) + "'";
				}
			} else {
				// 需求负责人
				tmp_content = tmp_content + "'',";
				// 需求提出部门
				// tmp_content = tmp_content + "'',";
				// 要求完成日期
				tmp_content = tmp_content + "'',";
				// 计划完成日
				tmp_content = tmp_content + "'',";
				// 实际完成日
				tmp_content = tmp_content + "'',";
				// 接收日
				tmp_content = tmp_content + "''";
			}
			if (i == array.length) {
				tmp_end = "]}";
			} else {
				tmp_end = "]},";
			}

			r_s = r_s + tmp_start + tmp_content + tmp_end;
		}
		r_s = r_start + r_s + r_end;
		return r_s;
	}

	@RequestMapping(value = "/query_staffer_form_send")
	public String query_test_form_send(HttpServletRequest request) throws ParseException {
		logger.info("access /staffer/query_staffer_form_send");

		String s_tmp = query_staffer_form_send_process(request);
		logger.info("query_staffer_form_send :" + s_tmp);
		return s_tmp;

	}

	private String query_staffer_form_send_process(HttpServletRequest request) throws ParseException {

		String test_workload_date_start = (String) request.getParameter("query_date_start");
		if(test_workload_date_start.length()<1){
			test_workload_date_start=null;
		}
		String test_workload_date_end = (String) request.getParameter("query_date_end");
		if(test_workload_date_end.length()<1){
			test_workload_date_end=null;
		}
		String project_status = (String) request.getParameter("project_status");
		String is_expire = (String) request.getParameter("is_expire");
		String users = (String) request.getParameter("req_analyst");
		String[] user_array = users.split(",");
		if(users.length()>1){
			user_array = users.split(",");
		}else{
			user_array = null;
		}
		
		/*
		 * Map<String, Object> map=new HashMap<String, Object>(); map.put("",
		 * "");
		 */
		// 获取全部test_bill
		List<Test_bill> all_test = test_bill_Service.search_all_test();
		// 过滤出合条件test_bill
		List<Test_bill> test_list = new ArrayList<Test_bill>();
		String value_expire = "1";
		for (Test_bill test : all_test) {
			boolean is_add = false;
			boolean is_add_1 = false;
			boolean is_check_project_status = false;
			boolean is_check_expire = false;
			String check_s="";
			if (!project_status.equals(check_s)) {
				is_check_project_status = true;
				if (test.getTest_status().equals(project_status)) {
					is_add_1 = true;
				}
			}
			Test_base_info test_base_info = test_bill_Service
					.search_a_test_base_info_by_testid(String.valueOf(test.getId()));
			boolean is_add_2 = false;
			if (is_expire.equals(value_expire)) {

				is_check_expire = true;
				if (test_base_info != null) {
					Date g_require_date = test.getTest_complete_date();

					if (test.getTest_status().equals(project_status)) {
						is_add_2 = true;
					}
				}

			} else {
				is_add_2 = true;
			}
			// 人员
			boolean is_add_3 = false;
			boolean is_check_user = false;
			if (user_array!=null) {
				is_check_user = true;
				if (test_base_info != null) {
					String g_test_head = test_base_info.getTest_head();
					String g_test_executor = test_base_info.getTest_executor();
					for (int i = 0; i < user_array.length; i++) {
						if (g_test_head != null) {
							if (g_test_head.contains(user_array[i])) {
								is_add_3 = true;
							}
						}
						if (g_test_head != null) {
							if (g_test_executor.contains(user_array[i])) {
								is_add_3 = true;
							}
						}
					}

				}

			}
			// 判断是否是组合查询
			//三个条件
			if (is_check_project_status && is_check_expire && is_check_user) {
				is_add = is_add_1 && is_add_2 && is_add_3;
			} else {
				//两条件
				if(is_check_project_status && is_check_expire ){
					is_add = is_add_1 && is_add_2 ;
				}
				if(is_check_expire && is_check_user){
					is_add = is_add_2 && is_add_3 ;
				}
				//is_add = is_add_1 || is_add_2 || is_add_3;
			}

			if (is_add) {
				test_list.add(test);
			}else{
				//一个条件
				if(is_check_project_status){
					is_add = is_add_1; 
				}
				if(is_check_expire){
					is_add = is_add_2; 
				}
				if(is_check_user){
					is_add = is_add_3; 
				}
				if (is_add) {
					test_list.add(test);
				}
			}

		}

		// 获取全部req_bill
		List<Req_bill> all_req = req_bill_Service.search_all_req_project();

		// 过滤出合条件req_bill
		List<Req_bill> req_list = new ArrayList<Req_bill>();

		for (Req_bill req : all_req) {
			boolean is_add = false;
			boolean is_add_1 = false;
			boolean is_check_project_status = false;
			boolean is_check_expire = false;
			if (project_status != null) {
				is_check_project_status = true;
				String get_status = req.getReq_status();
				if (get_status != null) {
					if (get_status.equals(project_status)) {
						is_add_1 = true;
					}
				}
			}
			boolean is_add_2 = false;
			Req_base_info req_base_info = req_bill_Service.search_req_base_info(String.valueOf(req.getId()));
			if (is_expire.equals(value_expire)) {
				
				is_check_expire = true;
				if (req_base_info != null) {
					Date g_require_date = req.getRequired_completion_date();

					if (req.getReq_status().equals(project_status)) {
						is_add_2 = true;
					}
				}

			} else {
				is_add_2 = true;
			}
			// 人员
						boolean is_add_3 = false;
						boolean is_check_user = false;
						if (user_array!=null) {
							is_check_user = true;
							if (req_base_info != null) {
								String g_req_head = req_base_info.getReq_head();
								String g_req_analyst = req_base_info.getReq_analyst();
								String g_req_supervisor = req_base_info.getReq_supervisor();
								for (int i = 0; i < user_array.length; i++) {
									if (g_req_head != null) {
										if (g_req_head.contains(user_array[i])) {
											is_add_3 = true;
										}
									}
									if (g_req_head != null) {
										if (g_req_analyst.contains(user_array[i])) {
											is_add_3 = true;
										}
									}
									if (g_req_supervisor != null) {
										if (g_req_supervisor.contains(user_array[i])) {
											is_add_3 = true;
										}
									}
								}

							}

						}
						// 判断是否是组合查询
						//三个条件
			// 判断是否是组合查询
			if (is_check_project_status && is_check_expire&&is_check_user) {
				is_add = is_add_1 && is_add_2&& is_add_3;
			} else {
				//两条件
				if(is_check_project_status && is_check_expire ){
					is_add = is_add_1 && is_add_2 ;
				}
				if(is_check_expire && is_check_user){
					is_add = is_add_2 && is_add_3 ;
				}
				//is_add = is_add_1 || is_add_2 || is_add_3;
			}

			if (is_add) {
				req_list.add(req);
			}else{
				//一个条件
				if(is_check_project_status){
					is_add = is_add_1; 
				}
				if(is_check_expire){
					is_add = is_add_2; 
				}
				if(is_check_user){
					is_add = is_add_3; 
				}
				if (is_add) {
					req_list.add(req);
				}
			}
		}

		// **************
		// ********测试项目结果计算
		// **************
		String test_r = "";
		if (test_list == null) {
			test_r = "0,0,0,0,0,tes_ids=";
		} else {

			int test_count = 0;
			test_count = test_list.size();

			// 实际工作量
			// （1）工作量计算日期之起始日<需求接收日 && （工作量计算日期之终止日>=需求接收日 &&
			// 工作量计算日期之终止日<=计划完成日）
			// （2）工作量计算日期之起始日>=需求接收日 && 工作量计算日期之终止日<=计划完成日
			// （3）（工作量计算日期之起始日>=需求接收日 && 工作量计算日期之起始日<=计划完成日） &&
			// 工作量计算日期之终止日>计划完成日
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			// 计划工作量
			double plan_workload_sum = 0.0;
			double actual_workload_sum = 0.0;
			int deffect_count = 0;
			int tc_count = 0;
			String test_ids = "";
			for (int i = 0; i < test_count; i++) {
				Test_bill test_bill = test_list.get(i);
				if (i == 0) {
					test_ids = test_ids + test_bill.getId();
				} else {
					test_ids = test_ids + "_" + test_bill.getId();
				}
				// 计算测试案例数
				tc_count = tc_count + count_test_case_count(test_bill.getId(),user_array);
				// 计算缺陷数
				deffect_count = deffect_count + count_test_deffect_count(test_bill.getId());
				/* if(req_workload_date_start.equals("")){ */
				// dt_req_workload_date_start 为空
				// 工作量计算 起始日取值 1990-01-01
				Date dt_test_workload_date_start = sdf.parse("1990-01-01");
				if (test_workload_date_start != null) {
					dt_test_workload_date_start = sdf.parse(test_workload_date_start);
				}
				// 工作量计算 终止日 取值
				Date dt_test_workload_date_end = sdf.parse("2099-01-01");
				if (test_workload_date_end != null) {
					dt_test_workload_date_end = sdf.parse(test_workload_date_end);
				}
				Date g_Accept_date = test_bill.getCreate_date();
				Test_base_info test_base_info = test_bill_Service
						.search_a_test_base_info_by_testid(String.valueOf(test_bill.getId()));
				// 计划完成时间设置，如果为空，取现在时间
				Date g_test_plan_date = sdf.parse("2099-01-01");
				if (test_base_info != null) {
					g_test_plan_date = test_base_info.getTest_plan_date();
				}

				// 计算计划工作量
				double r_plan_workload = count_plan_workload(g_Accept_date, g_test_plan_date,
						dt_test_workload_date_start, dt_test_workload_date_end, test_bill,user_array);
				plan_workload_sum = plan_workload_sum + r_plan_workload;

				// 实际完成时间
				// 实际完成时间设置，如果为空，取现在时间
				Date g_Req_actual_date = sdf.parse("2099-01-01");
				if (test_base_info != null) {
					g_Req_actual_date = test_base_info.getActual_finish_date();
					if (g_Req_actual_date == null) {
						actual_workload_sum = actual_workload_sum + 0.0;
					} else {
						// 计算实际工作量
						double r_actual_workload = count_actual_workload(g_Accept_date, g_Req_actual_date,
								dt_test_workload_date_start, dt_test_workload_date_end, test_bill,user_array);
						actual_workload_sum = actual_workload_sum + r_actual_workload;
					}

				} else {
					actual_workload_sum = actual_workload_sum + 0.0;
				}

			}

			test_r = test_count + "," + tc_count + "," + deffect_count + "," + plan_workload_sum + ","
					+ actual_workload_sum + ",test_ids=" + test_ids;
		}

		// **************
		// ********需求项目结果计算
		// **************
		String req_r = "";
		if (req_list == null) {
			req_r = "0,0,0,";
		} else {
			// req_count
			int req_count = 0;
			req_count = req_list.size();

			// 实际工作量
			// （1）工作量计算日期之起始日<需求接收日 && （工作量计算日期之终止日>=需求接收日 &&
			// 工作量计算日期之终止日<=计划完成日）
			// （2）工作量计算日期之起始日>=需求接收日 && 工作量计算日期之终止日<=计划完成日
			// （3）（工作量计算日期之起始日>=需求接收日 && 工作量计算日期之起始日<=计划完成日） &&
			// 工作量计算日期之终止日>计划完成日
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			// 计划工作量
			double plan_workload_sum = 0.0;
			double actual_workload_sum = 0.0;
			String req_ids = "";
			for (int i = 0; i < req_list.size(); i++) {
				Req_bill req_bill = req_list.get(i);
				if (i == 0) {
					req_ids = req_ids + req_bill.getId();
				} else {
					req_ids = req_ids + "_" + req_bill.getId();
				}
				/* if(req_workload_date_start.equals("")){ */
				// dt_req_workload_date_start 为空
				// 工作量计算 起始日取值 1990-01-01
				Date dt_req_workload_date_start = sdf.parse("1990-01-01");
				if (test_workload_date_start != null) {
					dt_req_workload_date_start = sdf.parse(test_workload_date_start);
				}
				// 工作量计算 终止日 取值
				Date dt_req_workload_date_end = sdf.parse("2099-01-01");
				if (test_workload_date_end != null) {
					dt_req_workload_date_end = sdf.parse(test_workload_date_end);
				}
				Date g_Accept_date = req_bill.getAccept_date();
				Req_base_info req_base_info = req_bill_Service.search_req_base_info(String.valueOf(req_bill.getId()));
				// 计划完成时间设置，如果为空，取现在时间
				// Date g_Req_plan_date = sdf.parse(sdf.format(new Date()));
				Date g_Req_plan_date = sdf.parse("2099-01-01");
				if (req_base_info != null) {
					g_Req_plan_date = req_base_info.getReq_plan_date();
				}

				// 计算计划工作量
				double r_plan_workload = count_plan_workload(g_Accept_date, g_Req_plan_date, dt_req_workload_date_start,
						dt_req_workload_date_end, req_bill,user_array);
				plan_workload_sum = plan_workload_sum + r_plan_workload;

				// 实际完成时间
				// 实际完成时间设置，如果为空，取现在时间
				Date g_Req_actual_date = sdf.parse("2099-01-01");
				if (req_base_info != null) {
					g_Req_actual_date = req_base_info.getActual_finish_date();
					if (g_Req_actual_date == null) {
						// actual_workload_sum=actual_workload_sum+0.0;
						g_Req_actual_date = sdf.parse("2099-01-01");
					}
					// 计算实际工作量
					double r_actual_workload = count_actual_workload(g_Accept_date, g_Req_actual_date,
							dt_req_workload_date_start, dt_req_workload_date_end, req_bill,user_array);
					actual_workload_sum = actual_workload_sum + r_actual_workload;
				} else {
					actual_workload_sum = actual_workload_sum + 0.0;
				}

			}

			req_r = req_count + "," + plan_workload_sum + "," + actual_workload_sum + ",req_ids=" + req_ids;
		}

		return test_r + ";" + req_r;
	}

	private int count_test_deffect_count(Integer id) {
		int return_value = 0;
		Test_testing_info testing_info = test_bill_Service.search_a_test_testing_info_by_testid(String.valueOf(id));
		if (testing_info != null) {
			return_value = testing_info.getDeffect_critical_count() + testing_info.getDeffect_ordinary_count()
					+ testing_info.getDeffect_other_count();
		}
		return return_value;
	}

	private double count_plan_workload(Date g_Accept_date, Date g_Req_plan_date, Date dt_req_workload_date_start,
			Date dt_req_workload_date_end, Test_bill test_bill, String[] user_array) throws ParseException {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");

		// create_working_Day(sdf.parse("2016-10-19"),sdf.parse("2017-10-19"));

		double r = 0.0;
		try {
			// （1）工作量计算日期之起始日<需求接收日
			if (dt_req_workload_date_start.getTime() < g_Accept_date.getTime()) {
				// &&工作量计算日期之终止日<=需求接收日
				if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
					// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-终止日）
					r = 0.0;
				} else {
					// &&工作量计算日期之终止日>需求接收日

					if (dt_req_workload_date_end.getTime() >= g_Req_plan_date.getTime()) {
						// &&工作量计算日期之终止日>=计划完成日
						// 工作量=项目或个人计划工作量
						//double g_4 = test_bill_Service.get_a_test_all_of_planworkload(test_bill.getId());
						r=get_a_test_all_of_planworkload_by_user_name(test_bill.getId(),user_array);
						//r = count1;

						/*
						 * long g_1=getDaySub(dt_req_workload_date_start,
						 * dt_req_workload_date_end); long
						 * g_2=getDaySub(g_Accept_date,dt_req_workload_date_end)
						 * ; double g_3=g_1/g_2; double
						 * g_4=req_bill_Service.get_a_req_all_of_planworkload(
						 * req_bill.getId()); r=g_3*g_4;
						 */

					} else {
						// &&工作量计算日期之终止日<计划完成日
						// 工作量=项目或个人计划工作量*（（需求接收日-终止日）/（需求接收日-计划完成日））

						// （（需求接收日-终止日）/（需求接收日-计划完成日））
						double g_1 = getDaySub(g_Accept_date, dt_req_workload_date_end);
						double g_2 = getDaySub(g_Accept_date, g_Req_plan_date);
						double g_3 = g_1 / g_2;
						//double g_4 = test_bill_Service.get_a_test_all_of_planworkload(test_bill.getId());
						double g_4 =get_a_test_all_of_planworkload_by_user_name(test_bill.getId(),user_array);
						r = g_3 * g_4;
						BigDecimal b = new BigDecimal(r);
						r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					}
				}

			}
			// （2）工作量计算日期之起始日 >= 需求接收日
			if (dt_req_workload_date_start.getTime() >= g_Accept_date.getTime()) {
				if (dt_req_workload_date_start.getTime() > g_Req_plan_date.getTime()) {
					// 工作量计算日期之起始日 > 计划完成日
					r = 0.0;
				} else {
					// &&工作量计算日期之起始日 <= 计划完成日

					// &&工作量计算日期之终止日>= 需求接收日
					if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
						r = 0.0;
					} else {
						// &&工作量计算日期之终止日<= 计划完成日
						if (dt_req_workload_date_end.getTime() <= g_Req_plan_date.getTime()) {
							// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, dt_req_workload_date_end);
							double g_2 = getDaySub(g_Accept_date, g_Req_plan_date);
							double g_3 = g_1 / g_2;
						//	double g_4 = test_bill_Service.get_a_test_all_of_planworkload(test_bill.getId());
							double g_4 =get_a_test_all_of_planworkload_by_user_name(test_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

						} else {
							// &&工作量计算日期之终止日> 计划完成日
							// 工作量=项目或个人计划工作量*（（起始-计划完成日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, g_Req_plan_date);
							double g_2 = getDaySub(g_Accept_date, g_Req_plan_date);
							double g_3 = g_1 / g_2;
							//double g_4 = test_bill_Service.get_a_test_all_of_planworkload(test_bill.getId());
							double g_4 =get_a_test_all_of_planworkload_by_user_name(test_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						}
					}
				}
			}

		} catch (Exception exception) {
			exception.printStackTrace();
			logger.info(exception.getMessage());
		}
		return r;

	}

	private double get_a_test_all_of_planworkload_by_user_name(Integer test_id, String[] user_array) {
		double count1=0.0;
		List<Test_workload_info> workload_list=test_bill_Service.search_test_workload_info(String.valueOf(test_id));
		if(workload_list!=null){
			for (Test_workload_info test_workload_info : workload_list) {
				String g_name=test_workload_info.getUser_name();
				String g_plan_workload=test_workload_info.getPlan_workload();
				String check_m="";
				if (g_plan_workload!=null) {
					if(!g_plan_workload.equals(check_m)){
						for (int i = 0; i < user_array.length; i++) {
							if(g_name.equals(user_array[i])){
								count1=count1+	Double.parseDouble(g_plan_workload);
							}
						}
					}
				}
				
			}
		}
		return count1;
	}

	private double count_actual_workload(Date g_Accept_date, Date g_Req_actual_date, Date dt_req_workload_date_start,
			Date dt_req_workload_date_end, Test_bill test_bill, String[] user_array) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		double r = 0.0;
		try {
			// （1）工作量计算日期之起始日<需求接收日
			if (dt_req_workload_date_start.getTime() < g_Accept_date.getTime()) {
				// &&工作量计算日期之终止日<=需求接收日
				if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
					// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-终止日）
					r = 0.0;
				} else {
					// &&工作量计算日期之终止日>需求接收日

					if (dt_req_workload_date_end.getTime() >= g_Req_actual_date.getTime()) {
						// &&工作量计算日期之终止日>=计划完成日
						// 工作量=项目或个人计划工作量
						double g_4 =get_a_test_all_of_actual_workload_by_user_name(test_bill.getId(),user_array);
						//double g_4 = test_bill_Service.get_a_test_all_of_actualworkload(test_bill.getId());
						r = g_4;

						/*
						 * long g_1=getDaySub(dt_req_workload_date_start,
						 * dt_req_workload_date_end); long
						 * g_2=getDaySub(g_Accept_date,dt_req_workload_date_end)
						 * ; double g_3=g_1/g_2; double
						 * g_4=req_bill_Service.get_a_req_all_of_planworkload(
						 * req_bill.getId()); r=g_3*g_4;
						 */

					} else {
						// &&工作量计算日期之终止日<计划完成日
						// 工作量=项目或个人计划工作量*（（需求接收日-终止日）/（需求接收日-计划完成日））

						// （（需求接收日-终止日）/（需求接收日-计划完成日））
						double g_1 = getDaySub(g_Accept_date, dt_req_workload_date_end);
						double g_2 = getDaySub(g_Accept_date, g_Req_actual_date);
						double g_3 = g_1 / g_2;
						//double g_4 = test_bill_Service.get_a_test_all_of_actualworkload(test_bill.getId());
						double g_4 =get_a_test_all_of_actual_workload_by_user_name(test_bill.getId(),user_array);
						r = g_3 * g_4;
						BigDecimal b = new BigDecimal(r);
						r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					}
				}

			}
			// （2）工作量计算日期之起始日 >= 需求接收日
			if (dt_req_workload_date_start.getTime() >= g_Accept_date.getTime()) {
				if (dt_req_workload_date_start.getTime() > g_Req_actual_date.getTime()) {
					// 工作量计算日期之起始日 > 计划完成日
					r = 0.0;
				} else {
					// &&工作量计算日期之起始日 <= 计划完成日

					// &&工作量计算日期之终止日>= 需求接收日
					if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
						r = 0.0;
					} else {
						// &&工作量计算日期之终止日<= 计划完成日
						if (dt_req_workload_date_end.getTime() <= g_Req_actual_date.getTime()) {
							// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, dt_req_workload_date_end);
							double g_2 = getDaySub(g_Accept_date, g_Req_actual_date);
							double g_3 = g_1 / g_2;
							//double g_4 = test_bill_Service.get_a_test_all_of_actualworkload(test_bill.getId());
							double g_4 =get_a_test_all_of_actual_workload_by_user_name(test_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

						} else {
							// &&工作量计算日期之终止日> 计划完成日
							// 工作量=项目或个人计划工作量*（（起始-计划完成日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, g_Req_actual_date);
							double g_2 = getDaySub(g_Accept_date, g_Req_actual_date);
							double g_3 = g_1 / g_2;
							//double g_4 = test_bill_Service.get_a_test_all_of_actualworkload(test_bill.getId());
							double g_4 =get_a_test_all_of_actual_workload_by_user_name(test_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						}
					}
				}
			}

		} catch (Exception exception) {
			exception.printStackTrace();
			logger.info(exception.getMessage());
		}
		return r;

	}

	private double get_a_test_all_of_actual_workload_by_user_name(Integer test_id, String[] user_array) {
		double count1=0.0;
		List<Test_workload_info> workload_list=test_bill_Service.search_test_workload_info(String.valueOf(test_id));
		if(workload_list!=null){
		for (Test_workload_info test_workload_info : workload_list) {
			String g_name=test_workload_info.getUser_name();
			String g_plan_workload=test_workload_info.getActual_workload();
			String check_m="";
			if (g_plan_workload!=null) {
				if(!g_plan_workload.equals(check_m)){
					for (int i = 0; i < user_array.length; i++) {
						if(g_name.equals(user_array[i])){
							count1=count1+	Double.parseDouble(g_plan_workload);
						}
					}
				}
			}
			
		}
		}
		return count1;
	}

	
	/**
	 * <li>功能描述：时间相减得到天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return long
	 * @author qzm
	 */
	public double getDaySub(Date beginDate, Date endDate) {
		double day = 0.0;
		day = query_Service.getDaySub(beginDate, endDate);
		// day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 *
		// 1000) +1;
		// System.out.println("相隔的天数="+day);
		return day;
	}

	public void create_working_Day(Date beginDate, Date endDate) {
		Calendar calendar = new GregorianCalendar();

		Date d = beginDate;
		calendar.setTime(d);

		for (int i = 0; beginDate.getTime() < endDate.getTime(); i++) {

			calendar.setTime(d);
			calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
			Date date_tmp = calendar.getTime(); // 这个时间就是日期往后推一天的结果
			logger.info("calendar=" + sdf.format(date_tmp) + " i=" + i);
			// 判断时间是否是星期六日
			SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
			String g_tmp = dateFm.format(date_tmp);
			logger.info("g_tmp=" + sdf.format(date_tmp) + " " + g_tmp);

			String check_value1 = "星期六";
			String check_value2 = "星期日";
			boolean is_add = true;
			if (g_tmp.equals(check_value1)) {
				is_add = false;
			}
			if (g_tmp.equals(check_value2)) {
				is_add = false;
			}
			if (is_add) {
				query_Service.add_a_date_as_working_date(date_tmp);
			}
			beginDate = date_tmp;

		}

	}

	private int count_test_case_count(Integer id, String[] user_array) {
		int return_value = 0;
		List<Test_workload_info> workload_info = test_bill_Service.search_test_workload_info(String.valueOf(id));
		if (workload_info != null) {
			for (Test_workload_info test_workload_info : workload_info) {
				//user_array
				String g_user_name=test_workload_info.getUser_name();
				for (int i = 0; i < user_array.length; i++) {
					if(g_user_name.equals(user_array[i])){
						return_value = return_value + test_workload_info.getBatch_testcase_count()
						+ test_workload_info.getOnline_testcase_count();
					}
				}
				
				
			}

		}
		return return_value;
	}

	
	private double get_a_req_all_of_actual_workload_by_user_name(Integer req_id, String[] user_array) {
		double count1=0.0;
		List<Req_workload_info> workload_list=req_bill_Service.search_req_workload_info(String.valueOf(req_id));
		if(workload_list!=null){
			for (Req_workload_info req_workload_info : workload_list) {
				String g_name=req_workload_info.getUser_name();
				String g_plan_workload=req_workload_info.getActual_workload();
				String check_m="";
				if (g_plan_workload!=null) {
					if(!g_plan_workload.equals(check_m)){
						for (int i = 0; i < user_array.length; i++) {
							if(g_name.equals(user_array[i])){
								count1=count1+	Double.parseDouble(g_plan_workload);
							}
						}
					}
				}
				
			}
		}
		return count1;
	}
	private double get_a_req_all_of_plan_workload_by_user_name(Integer req_id, String[] user_array) {
		double count1=0.0;
		List<Req_workload_info> workload_list=req_bill_Service.search_req_workload_info(String.valueOf(req_id));
		if(workload_list!=null){
			for (Req_workload_info req_workload_info : workload_list) {
				String g_name=req_workload_info.getUser_name();
				String g_plan_workload=req_workload_info.getPlan_workload();
				String check_m="";
				if (g_plan_workload!=null) {
					if(!g_plan_workload.equals(check_m)){
						for (int i = 0; i < user_array.length; i++) {
							if(g_name.equals(user_array[i])){
								count1=count1+	Double.parseDouble(g_plan_workload);
							}
						}
					}
				}
				
			}
		}
		return count1;
	}
	
	private double count_plan_workload(Date g_Accept_date, Date g_Req_plan_date, Date dt_req_workload_date_start,
			Date dt_req_workload_date_end, Req_bill req_bill,String[] user_array) throws ParseException {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		logger.info("req_bill id =" + req_bill.getId());
		logger.info("dt_req_workload_date_start =" + df.format(dt_req_workload_date_start));
		logger.info("g_Accept_date =" + df.format(g_Accept_date));
		// create_working_Day(sdf.parse("2016-10-19"),sdf.parse("2017-10-19"));

		double r = 0.0;
		try {
			// （1）工作量计算日期之起始日<需求接收日
			if (dt_req_workload_date_start.getTime() < g_Accept_date.getTime()) {
				// &&工作量计算日期之终止日<=需求接收日
				if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
					// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-终止日）
					r = 0.0;
				} else {
					// &&工作量计算日期之终止日>需求接收日

					if (dt_req_workload_date_end.getTime() >= g_Req_plan_date.getTime()) {
						// &&工作量计算日期之终止日>=计划完成日
						// 工作量=项目或个人计划工作量
						//double g_4 = req_bill_Service.get_a_req_all_of_planworkload(req_bill.getId());
						double g_4 = get_a_req_all_of_plan_workload_by_user_name(req_bill.getId(),user_array);
						r = g_4;

						/*
						 * long g_1=getDaySub(dt_req_workload_date_start,
						 * dt_req_workload_date_end); long
						 * g_2=getDaySub(g_Accept_date,dt_req_workload_date_end)
						 * ; double g_3=g_1/g_2; double
						 * g_4=req_bill_Service.get_a_req_all_of_planworkload(
						 * req_bill.getId()); r=g_3*g_4;
						 */

					} else {
						// &&工作量计算日期之终止日<计划完成日
						// 工作量=项目或个人计划工作量*（（需求接收日-终止日）/（需求接收日-计划完成日））

						// （（需求接收日-终止日）/（需求接收日-计划完成日））
						double g_1 = getDaySub(g_Accept_date, dt_req_workload_date_end);
						double g_2 = getDaySub(g_Accept_date, g_Req_plan_date);
						double g_3 = g_1 / g_2;
						//double g_4 = req_bill_Service.get_a_req_all_of_planworkload(req_bill.getId());
						double g_4 = get_a_req_all_of_plan_workload_by_user_name(req_bill.getId(),user_array);
						r = g_3 * g_4;
						BigDecimal b = new BigDecimal(r);
						r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					}
				}

			}
			// （2）工作量计算日期之起始日 >= 需求接收日
			if (dt_req_workload_date_start.getTime() >= g_Accept_date.getTime()) {
				if (dt_req_workload_date_start.getTime() > g_Req_plan_date.getTime()) {
					// 工作量计算日期之起始日 > 计划完成日
					r = 0.0;
				} else {
					// &&工作量计算日期之起始日 <= 计划完成日

					// &&工作量计算日期之终止日>= 需求接收日
					if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
						r = 0.0;
					} else {
						// &&工作量计算日期之终止日<= 计划完成日
						if (dt_req_workload_date_end.getTime() <= g_Req_plan_date.getTime()) {
							// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, dt_req_workload_date_end);
							double g_2 = getDaySub(g_Accept_date, g_Req_plan_date);
							double g_3 = g_1 / g_2;
							//double g_4 = req_bill_Service.get_a_req_all_of_planworkload(req_bill.getId());
							double g_4 = get_a_req_all_of_plan_workload_by_user_name(req_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

						} else {
							// &&工作量计算日期之终止日> 计划完成日
							// 工作量=项目或个人计划工作量*（（起始-计划完成日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, g_Req_plan_date);
							double g_2 = getDaySub(g_Accept_date, g_Req_plan_date);
							double g_3 = g_1 / g_2;
							//double g_4 = req_bill_Service.get_a_req_all_of_planworkload(req_bill.getId());
							double g_4 = get_a_req_all_of_plan_workload_by_user_name(req_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						}
					}
				}
			}

		} catch (Exception exception) {
			exception.printStackTrace();
			logger.info(exception.getMessage());
		}
		return r;

	}

	private double count_actual_workload(Date g_Accept_date, Date g_Req_actual_date, Date dt_req_workload_date_start,
			Date dt_req_workload_date_end, Req_bill req_bill,String[] user_array) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		double r = 0.0;
		try {
			// （1）工作量计算日期之起始日<需求接收日
			if (dt_req_workload_date_start.getTime() < g_Accept_date.getTime()) {
				// &&工作量计算日期之终止日<=需求接收日
				if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
					// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-终止日）
					r = 0.0;
				} else {
					// &&工作量计算日期之终止日>需求接收日

					if (dt_req_workload_date_end.getTime() >= g_Req_actual_date.getTime()) {
						// &&工作量计算日期之终止日>=计划完成日
						// 工作量=项目或个人计划工作量
						//double g_4 = req_bill_Service.get_a_req_all_of_actualworkload(req_bill.getId());
						double g_4 = get_a_req_all_of_actual_workload_by_user_name(req_bill.getId(),user_array);
						r = g_4;

						/*
						 * long g_1=getDaySub(dt_req_workload_date_start,
						 * dt_req_workload_date_end); long
						 * g_2=getDaySub(g_Accept_date,dt_req_workload_date_end)
						 * ; double g_3=g_1/g_2; double
						 * g_4=req_bill_Service.get_a_req_all_of_planworkload(
						 * req_bill.getId()); r=g_3*g_4;
						 */

					} else {
						// &&工作量计算日期之终止日<计划完成日
						// 工作量=项目或个人计划工作量*（（需求接收日-终止日）/（需求接收日-计划完成日））

						// （（需求接收日-终止日）/（需求接收日-计划完成日））
						double g_1 = getDaySub(g_Accept_date, dt_req_workload_date_end);
						double g_2 = getDaySub(g_Accept_date, g_Req_actual_date);
						double g_3 = g_1 / g_2;
						//double g_4 = req_bill_Service.get_a_req_all_of_actualworkload(req_bill.getId());
						double g_4 = get_a_req_all_of_actual_workload_by_user_name(req_bill.getId(),user_array);
						r = g_3 * g_4;
						BigDecimal b = new BigDecimal(r);
						r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					}
				}

			}
			// （2）工作量计算日期之起始日 >= 需求接收日
			if (dt_req_workload_date_start.getTime() >= g_Accept_date.getTime()) {
				if (dt_req_workload_date_start.getTime() > g_Req_actual_date.getTime()) {
					// 工作量计算日期之起始日 > 计划完成日
					r = 0.0;
				} else {
					// &&工作量计算日期之起始日 <= 计划完成日

					// &&工作量计算日期之终止日>= 需求接收日
					if (dt_req_workload_date_end.getTime() <= g_Accept_date.getTime()) {
						r = 0.0;
					} else {
						// &&工作量计算日期之终止日<= 计划完成日
						if (dt_req_workload_date_end.getTime() <= g_Req_actual_date.getTime()) {
							// 工作量=项目或个人计划工作量*（（起始日-终止日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, dt_req_workload_date_end);
							double g_2 = getDaySub(g_Accept_date, g_Req_actual_date);
							double g_3 = g_1 / g_2;
							//double g_4 = req_bill_Service.get_a_req_all_of_actualworkload(req_bill.getId());
							double g_4 = get_a_req_all_of_actual_workload_by_user_name(req_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

						} else {
							// &&工作量计算日期之终止日> 计划完成日
							// 工作量=项目或个人计划工作量*（（起始-计划完成日）/（需求接收日-计划完成日））
							double g_1 = getDaySub(dt_req_workload_date_start, g_Req_actual_date);
							double g_2 = getDaySub(g_Accept_date, g_Req_actual_date);
							double g_3 = g_1 / g_2;
							//double g_4 = req_bill_Service.get_a_req_all_of_actualworkload(req_bill.getId());
							double g_4 = get_a_req_all_of_actual_workload_by_user_name(req_bill.getId(),user_array);
							r = g_3 * g_4;
							BigDecimal b = new BigDecimal(r);
							r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						}
					}
				}
			}

		} catch (Exception exception) {
			exception.printStackTrace();
			logger.info(exception.getMessage());
		}
		return r;

	}

}
